<?php

namespace App\Http\Controllers;

use App\Exceptions\ReportableException;
use App\Http\Requests\UserPostRequest;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    //
    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): \Illuminate\Http\JsonResponse
    {
        $users = new User();
        $users  = $users->index($request);
        return response()->json(['data' => $users]);
    }

    /**
     * 用户详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id): \Illuminate\Http\JsonResponse
    {
        $user = User::find($id);
        return response()->json(['data' => $user]);
    }

    /**
     * 新增用户
     * @param UserPostRequest $request
     * @return JsonResponse
     */
    public function store(UserPostRequest $request): \Illuminate\Http\JsonResponse
    {
        // 新增用户
        $userData = $request->safe()->only(['name', 'email', 'password','status']);
        if ($request->has('password')){
            $userData['password'] = Hash::make($userData['password']);
        }
        $roleIds = $request->safe()->input('roleIds');
        $departmentIds = $request->safe()->input('departmentIds');
        $user = User::create($userData);
        // 将roleIds 于用户关联，多对多，插入到user_role表
        $user->roles()->sync($roleIds);
        // 将departmentIds 于用户关联
        $user->department()->sync($departmentIds);
        return response()->json(['message' => '新增用户成功']);
    }

    /** 更新用户
     * @param UserPostRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function update(UserPostRequest $request, $id): \Illuminate\Http\JsonResponse
    {
        $user = User::find($id);
        if (!$user) {
            return response()->json(['message' => '用户不存在'], ReportableException::INVALID_CODE);
        }
        $userData = $request->safe()->only(['name', 'email', 'status']);
        $user->fill($userData);
        $user->update();
        if ($request->has('roleIds')) {
            $roleIds = $request->safe()->input('roleIds');
            $user->roles()->sync($roleIds);
        }
        if ($request->has('departmentIds')) {
            $departmentIds = $request->safe()->input('departmentIds');
            $user->departments()->sync($departmentIds);
        }
        return response()->json(['message' => '更新用户成功']);
    }

    /** 删除用户
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        // 不能删除自己和超级管理员
        $user_id = $request->attributes->get('user_id');
        if ($user_id == $id) {
            return response()->json(['message' => '不能删除自己'], ReportableException::INVALID_CODE);
        }
        // 超级管理员
        if ($id == 1) {
            return response()->json(['message' => '不能删除超级管理员'], ReportableException::INVALID_CODE);
        }
        User::destroy($id);
        return response()->json(['message' => '删除用户成功']);
    }

    public function reset(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        $user = User::find($id);
        $validate = $request->validate([
            'password' => 'required|string|min:6',
        ], [
            'password.required' => '密码不能为空',
            'password.string' => '密码格式错误',
            'password.min' => '密码长度不能小于6位',
        ]);
        if (!$validate) {
            return response()->json(['message' => '密码格式错误'], ReportableException::INVALID_CODE);
        }
        $user->password = Hash::make($request->input('password'));
        $user->save();
        return response()->json(['message' => '重置密码成功']);
    }
}
